import { defineStore } from 'pinia';
import { getEmployees, createEmployee, updateEmployee, deleteEmployee } from '@//services/employeeService'; //from后的路径一定要正确（对应employeeService）

export const useEmployeeStore = defineStore('employee', {
  state: () => ({
    employees: [],
    searchParams: {
      name: '',
      department: '',
      startDate: null,
      endDate: null
    },
    isLoading: false,
    error: null
  }),
  actions: {
    async fetchEmployees() {
      this.isLoading = true;
      this.error = null;
      try {
        const response = await getEmployees();
        this.employees = response.data;
      } catch (error) {
        console.error('获取员工列表失败:', error);
        this.error = error.message;
      } finally {
        this.isLoading = false;
      }
    },
    async addNewEmployee(employeeData) {
      try {
        await createEmployee(employeeData);
        await this.fetchEmployees();
      } catch (error) {
        console.error('添加员工失败:', error);
      }
    },
    async updateEmployee(id, updates) {
      try {
        await updateEmployee(id, updates);
        await this.fetchEmployees();
      } catch (error) {
        console.error('更新员工失败:', error);
      }
    },
    async deleteEmployee(id) {
      try {
        await deleteEmployee(id);
        await this.fetchEmployees();
      } catch (error) {
        console.error('删除员工失败:', error);
      }
    }
  }
});